package com.eclink.asset.workflow.listener;


import com.eclink.asset.asset.handler.*;
import com.eclink.asset.common.exception.BusinessException;
import com.eclink.asset.consume.handler.ConsumeApplyDispatchHandler;
import com.eclink.asset.consume.handler.ConsumeApplyInHandler;
import com.eclink.asset.workflow.domain.dao.Flow;
import com.eclink.asset.workflow.event.ApplyApproveEvent;
import com.eclink.asset.workflow.event.ApplyRejectEvent;
import com.eclink.asset.workflow.handler.ApplyFinishHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ApplyApproveListener {

    @Resource
    private ApplicationContext applicationContext;

    /**
     * 监听
     * @param event 申请审批完成事件
     */
    @EventListener
    public void handleApplyRejectEvent(ApplyRejectEvent event) {
        Flow flow = event.getFlow();
        String applyType = flow.getApplyType();

        ApplyFinishHandler handler = null;
        if ("ZCRK".equals(applyType)) {
            handler = applicationContext.getBean(AssetApplyInHandler.class);
        } else if ("ZCPF".equals(applyType)) {
            handler = applicationContext.getBean(AssetApplyDispatchHandler.class);
        } else if ("ZCJC".equals(applyType)) {
            handler = applicationContext.getBean(AssetApplyBorrowHandler.class);
        } else if ("ZCGH".equals(applyType) || "ZCTK".equals(applyType)) {
            handler = applicationContext.getBean(AssetReturnFinishHandler.class);
        } else if ("ZCDB".equals(applyType)) {
            handler = applicationContext.getBean(AssetTransferFinishHandler.class);
        } else if ("ZCCL".equals(applyType)) {
            handler = applicationContext.getBean(AssetHandleFinishHandler.class);
        } else if ("ZCWX".equals(applyType)) {
            handler = applicationContext.getBean(AssetRepairFinishHandler.class);
        } else if ("ZCLY".equals(applyType)) {
            handler = applicationContext.getBean(EmployeeAssetApplyReceiveHandler.class);
        } else if ("HCRK".equals(applyType)) {
            handler = applicationContext.getBean(ConsumeApplyInHandler.class);
        } else if ("HCPF".equals(applyType)) {
            handler = applicationContext.getBean(ConsumeApplyDispatchHandler.class);
        }
        if (handler!=null) {
            handler.reject(flow.getApplyId());
        }

    }

    /**
     * 监听
     * @param event 申请审批完成事件
     */
    @EventListener
    public void handleApplyApproveEvent(ApplyApproveEvent event) {
        Flow flow = event.getFlow();
        String applyType = flow.getApplyType();

        ApplyFinishHandler handler = null;
        if ("ZCRK".equals(applyType)) {
            handler = applicationContext.getBean(AssetApplyInHandler.class);
        } else if ("ZCPF".equals(applyType)) {
            handler = applicationContext.getBean(AssetApplyDispatchHandler.class);
        } else if ("ZCJC".equals(applyType)) {
            handler = applicationContext.getBean(AssetApplyBorrowHandler.class);
        } else if ("ZCGH".equals(applyType) || "ZCTK".equals(applyType)) {
            handler = applicationContext.getBean(AssetReturnFinishHandler.class);
        } else if ("ZCDB".equals(applyType)) {
            handler = applicationContext.getBean(AssetTransferFinishHandler.class);
        } else if ("ZCPD".equals(applyType)) {
            handler = applicationContext.getBean(AssetInventoryFinishHandler.class);
        } else if ("ZCCL".equals(applyType)) {
            handler = applicationContext.getBean(AssetHandleFinishHandler.class);
        } else if ("ZCWX".equals(applyType)) {
            handler = applicationContext.getBean(AssetRepairFinishHandler.class);
        } else if ("ZCLY".equals(applyType)) {
            handler = applicationContext.getBean(EmployeeAssetApplyReceiveHandler.class);
        } else if ("HCRK".equals(applyType)) {
            handler = applicationContext.getBean(ConsumeApplyInHandler.class);
        } else if ("HCPF".equals(applyType)) {
            handler = applicationContext.getBean(ConsumeApplyDispatchHandler.class);
        }
        if (handler==null) {
            throw new BusinessException("这个申请没有定义处理操作");
        }
        if (handler!=null) {
            handler.execute(flow.getApplyId());
        }

    }

}
